package cpr.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import cpr.Utils;
import cpr.context.SolveContext;


public class SolveBestRulesEntry {
    public int uncovered;
    public AREntry[] ares;
    private SolveContext ctx;
    
    public SolveBestRulesEntry(SolveContext ctx) {
    	this.ctx = ctx;
    	
    }
    
    public SolveBestRulesEntry(SolveContext ctx, SolveBestRulesEntry toCopy, AREntryComparator comparator) {
    	this.ctx = ctx;
    	this.uncovered = toCopy.uncovered;
    	ares = new AREntry[toCopy.ares.length];
    	SortedSet ss = new TreeSet(new AREntryComparator());
        
        for (int i = 0; i < toCopy.ares.length; i++) {
            ss.add(toCopy.ares[i]);
        } 
        if (ares.length!=ss.size()) {
        	throw new RuntimeException("duplicate rules!");
        }
        int i =0;
        for (Iterator iter = ss.iterator(); iter.hasNext();) {
			AREntry a = (AREntry) iter.next();
			ares[i++]= a;
		}
    	
    }
    
    public String toString() {
    	try{
	    	List printed = new ArrayList();
			int cc = 0;
	        StringBuffer sb = new StringBuffer();
	        SortedSet ss = new TreeSet(new AREntryComparator());
	        
	        for (int i = 0; i < ares.length; i++) {
	            ss.add(ares[i]);
	        } 
	        if (ares.length!=ss.size()) {
	        	throw new RuntimeException("duplicate rules!");
	        }
	        int nr = 0;
	        for (Iterator iter = ss.iterator(); iter.hasNext();) {
				AREntry a = (AREntry) iter.next();
				if (!ctx.is_in(a, printed)) {
					String rstr = ctx.getContext().getPrintContext().printRuleUsingDict(a.rule, a.vector);
					String attrValue = ctx.getContext().getPrintContext().getStringAttrValue(a.rule,true);
	                //int s = Utils.printRuleAccordingToVectorDigital(a.rule, a.vector);
	                sb.append(nr+". "+a.rule.ruleNr+"/"+a.rule.ruleNrGlobal+" ["+Utils.getScore(a.vector)+"] "+ rstr+" ->"+attrValue+"\n");
	                printed.add(a);
	                cc++;
	                
	            } else {
	            	throw new RuntimeException("what is_in means here???");
	            }
				nr++;
			}
			return sb.toString();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		
    }
}
